package com.yan.furns.dao.impl;

import com.yan.furns.dao.BasicDAO;
import com.yan.furns.dao.FurnDAO;
import com.yan.furns.entity.Furn;

import java.util.List;

/**
 * @author Yzb
 * @version 1.0
 */
public class FurnDAOImpl extends BasicDAO implements FurnDAO {
	@Override
	public List<Furn> queryFurns() {
		String sql = "select `id`, `name`, `maker`, " +
				"`price`, `sales`, `stock`, `img_path` imgPath from furn";
		return queryMulti(sql, Furn.class);
	}

	@Override
	public int insertFurn(Furn furn) {
		String sql = "insert into furn (name, maker, price, sales, stock, img_path) values (?,?,?,?,?,?)";
		return update(sql, furn.getName(), furn.getMaker(), furn.getPrice(), furn.getSales(), furn.getStock(), furn.getImgPath());
	}

	@Override
	public int deleteFurn(int id) {
		String sql = "delete from furn where id = ?";
		return update(sql, id);
	}

	@Override
	public int updateFurn(Furn furn) {
		String sql = "updatex furn set name=?, maker=?, price=?, sales=?, stock=?, img_path=? where id=?";
		return update(sql, furn.getName(), furn.getMaker(), furn.getPrice(), furn.getSales(), furn.getStock(), furn.getImgPath(), furn.getId());
	}

	@Override
	public Furn queryFurnById(int id) {
		String sql = "select id, name, maker, price, sales, stock, img_path as imgPath from furn where id=?";
		return (Furn) querySingle(sql, Furn.class, id);
	}

	@Override
	public List<Furn> pageQueryFurn(int begin, int pageSize) {
//		Integer offset = (pageNo - 1) * pageSize;
		String sql = "select `id`, `name`, `maker`, " +
				"`price`, `sales`, `stock`, `img_path` imgPath from furn " +
				"limit ?, ?";
		return queryMulti(sql, Furn.class, begin, pageSize);
	}

	@Override
	public Integer queryTotalFurns() {
		String sql = "select count(*) from furn";
		return ((Number) queryScalar(sql)).intValue();
	}

	@Override
	public List<Furn> pageQueryFurnByName(String name, int begin, int pageSize) {
		String sql = "select `id`, `name`, `maker`, " +
				"`price`, `sales`, `stock`, `img_path` imgPath from furn where name like ? " +
				"limit ?, ?";
		return queryMulti(sql, Furn.class, "%" + name + "%", begin, pageSize);
	}

	@Override
	public Integer queryTotalFurnsByName(String name) {
		String sql = "select count(*) from furn where name like ?";
		return ((Number) queryScalar(sql, "%" + name + "%")).intValue();
	}
}